/**
 * Created by XianTao on 2016/11/7
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.handle.exception.restful.admin;

import org.lego.handle.exception.ExtraInvalidParameterException;
import org.lego.handle.exception.configuration.ConstantMessageKey;
import org.lego.handle.exception.entity.ExceptionMessage;
import org.lego.handle.exception.service.ExceptionMessageService;
import org.lego.jpa.base.util.DynamicSpecifications;
import org.lego.jpa.base.util.ServletUtil;
import org.lego.jpa.base.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.security.InvalidParameterException;

@RestController
@RequestMapping(value = "/admin/exception/message", method = RequestMethod.POST)
public class ExceptionMessageAdminRestful {
    @Autowired
    private ExceptionMessageService exceptionMessageService;

    @RequestMapping(value = "/list")
    public ResultVo<Page<ExceptionMessage>> list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                                                 @RequestParam(value = "rows", defaultValue = "10") int pageSize,
                                                 ServletRequest request) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, ServletUtil.getParametersStartingWithSort(request));
        Specification<ExceptionMessage> specification = DynamicSpecifications.buildSpecification(
                ServletUtil.getParametersStartingWithSearch(request), ExceptionMessage.class);
        return new ResultVo<Page<ExceptionMessage>>(exceptionMessageService.findAll(specification, pageRequest));
    }

    @RequestMapping(value = "/insertOne")
    public ResultVo<ExceptionMessage> insertOne(@RequestBody ExceptionMessage exceptionMessage) {
        return new ResultVo<ExceptionMessage>(exceptionMessageService.saveAndFlush(exceptionMessage));
    }

    @RequestMapping(value = "/updateOne")
    public ResultVo<ExceptionMessage> updateOne(@RequestBody ExceptionMessage exceptionMessage) {
        if (StringUtils.isEmpty(exceptionMessage.getId())) {
            throw new InvalidParameterException(ConstantMessageKey.EXCEPTION_MESSAGE_NEED_PARAM_ID);
        }
        ExceptionMessage currentOne = exceptionMessageService.findOne(exceptionMessage.getId());
        if (currentOne == null) {
            throw new ExtraInvalidParameterException(ConstantMessageKey.EXCEPTION_MESSAGE_NOT_FOUND_BY_ID, exceptionMessage.getId());
        }
        if (StringUtils.hasText(exceptionMessage.getMessage())) {
            currentOne.setMessage(exceptionMessage.getMessage());
        }
        return new ResultVo<ExceptionMessage>(exceptionMessageService.saveAndFlush(currentOne));
    }
}
